package cn.com.ebidding.spc.controller;

import cn.hutool.core.util.RandomUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created on 2020/10/21.
 *
 * @author qiyang.
 * 描述：
 */
@Slf4j
@RestController
public class HystrixTestController {

    @GetMapping("/test/hystrix/timeout")
    @HystrixCommand(fallbackMethod = "fallbackMethodCall", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),   //是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),  //请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),    //时间窗口期 10秒
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),    //失败率达到多少后跳闸
    })
    public String timeOut(){

        log.info("进入/test/hystrix/timeout");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "SUCCESS:" + RandomUtil.randomString(18);
    }

    private String fallbackMethodCall(){
        log.warn("FAIL:进入服务降级处理");
        return "FAIL";
    }

}
